{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出9x9乘法口诀表。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1*1=1\t\n",
      "1*2=2\t2*2=4\t\n",
      "1*3=3\t2*3=6\t3*3=9\t\n",
      "1*4=4\t2*4=8\t3*4=12\t4*4=16\t\n",
      "1*5=5\t2*5=10\t3*5=15\t4*5=20\t5*5=25\t\n",
      "1*6=6\t2*6=12\t3*6=18\t4*6=24\t5*6=30\t6*6=36\t\n",
      "1*7=7\t2*7=14\t3*7=21\t4*7=28\t5*7=35\t6*7=42\t7*7=49\t\n",
      "1*8=8\t2*8=16\t3*8=24\t4*8=32\t5*8=40\t6*8=48\t7*8=56\t8*8=64\t\n",
      "1*9=9\t2*9=18\t3*9=27\t4*9=36\t5*9=45\t6*9=54\t7*9=63\t8*9=72\t9*9=81\t\n"
     ]
    }
   ],
   "source": [
    "for i in range(1, 10):\n",
    "    for j in range(1, i + 1):\n",
    "        print(f\"{j}*{i}={i * j}\\t\", end=\"\")\n",
    "    print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输入一个任意正整数，输出分解质因数。例如：输入90,打印出90=2*3*3*5。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1分解质因数的结果为: 2 * 3 * 3 * 5\n"
     ]
    }
   ],
   "source": [
    "n = int(input(\"请输入一个正整数: \"))\n",
    "prime_factors = []\n",
    "divisor = 2\n",
    "while n > 1:\n",
    "    while n % divisor == 0:\n",
    "        prime_factors.append(divisor)\n",
    "        n //= divisor\n",
    "    divisor += 1\n",
    "print(f\"{n}分解质因数的结果为: \", end=\"\")\n",
    "for index, factor in enumerate(prime_factors):\n",
    "    if index < len(prime_factors) - 1:\n",
    "        print(factor, end=\" * \")\n",
    "    else:\n",
    "        print(factor)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输入一个不多于6位的正整数，要求：一、求它是几位数，二、逆序打印出各位数字。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该数字是2位数\n",
      "逆序打印各位数字为: 32\n"
     ]
    }
   ],
   "source": [
    "num = input(\"请输入一个不多于6位的正整数: \")\n",
    "if not num.isdigit() or int(num) <= 0 or len(num) > 6:\n",
    "    print(\"输入不合法，请输入不多于6位的正整数\")\n",
    "else:\n",
    "    digit_count = len(num)\n",
    "    print(f\"该数字是{digit_count}位数\")\n",
    "    print(\"逆序打印各位数字为:\", num[::-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "求s=a+aa+aaa+aaaa+aa...a的值，其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加)，几个数相加有键盘控制。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "结果为: 8641969\n"
     ]
    }
   ],
   "source": [
    "\n",
    "a = input(\"请输入一个数字（0 - 9）: \")\n",
    "if not (a.isdigit() and len(a) == 1):\n",
    "    print(\"输入不合法，请输入一个0 - 9之间的数字\")\n",
    "else:\n",
    "    n = input(\"请输入相加的项数: \")\n",
    "    if not n.isdigit():\n",
    "        print(\"项数输入不合法，请输入一个正整数\")\n",
    "    else:\n",
    "        n = int(n)\n",
    "        sum_result = 0\n",
    "        for i in range(1, n + 1):\n",
    "            num_str = a * i\n",
    "            sum_result += int(num_str)\n",
    "        print(f\"结果为: {sum_result}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "有一分数序列：2/1，3/2，5/3，8/5，13/8，21/13...求出这个数列的前N项之和，N由键盘输入。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该数列前8项之和为: 13.24374595992243\n"
     ]
    }
   ],
   "source": [
    "N = input(\"请输入项数N（N为正整数）: \")\n",
    "if not N.isdigit() or int(N) <= 0:\n",
    "    print(\"输入不合法，请输入正整数作为项数\")\n",
    "else:\n",
    "    N = int(N)\n",
    "    sum_result = 0\n",
    "    numerator = 2\n",
    "    denominator = 1\n",
    "    for _ in range(N):\n",
    "        sum_result += numerator / denominator\n",
    "        new_numerator = numerator + denominator\n",
    "        new_denominator = numerator\n",
    "        numerator = new_numerator\n",
    "        denominator = new_denominator\n",
    "    print(f\"该数列前{N}项之和为: {sum_result}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "一个整数，它加上100后是一个完全平方数，再加上268又是一个完全平方数，请问该数是多少？\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-99\n",
      "-96\n",
      "4389\n",
      "17856\n"
     ]
    }
   ],
   "source": [
    "for n1 in range(1, 1000): \n",
    "    n2 = (268 / (n1 + n1)) + n1\n",
    "    if n2 == int(n2):\n",
    "        m = n1 ** 2 - 100\n",
    "        print(m)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "有一对兔子，从出生后第3个月起每个月都生一对兔子，小兔子长到第三个月后每个月又生一对兔子，假如兔子都不死，问每个月的兔子总数为多少？\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第5个月兔子的总数是5对\n"
     ]
    }
   ],
   "source": [
    "month = int(input(\"请输入月数: \"))\n",
    "if month < 1:\n",
    "    print(\"请输入大于等于1的月数\")\n",
    "else:\n",
    "    f1 = 1\n",
    "    f2 = 1\n",
    "    if month == 1 or month == 2:\n",
    "        print(f\"第{month}个月兔子的总数是1对\")\n",
    "    else:\n",
    "        for _ in range(3, month + 1):\n",
    "            fn = f1 + f2\n",
    "            f1 = f2\n",
    "            f2 = fn\n",
    "        print(f\"第{month}个月兔子的总数是{fn}对\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "任意输入一个3x3矩阵，求矩阵对角线元素之和。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该矩阵对角线元素之和为: 30\n"
     ]
    }
   ],
   "source": [
    "matrix = []\n",
    "for i in range(3):\n",
    "    row = input(f\"请输入矩阵第{i + 1}行的3个元素（元素之间用空格隔开）: \").split()\n",
    "    row = [int(num) for num in row]\n",
    "    matrix.append(row)\n",
    "\n",
    "sum_diagonal = 0\n",
    "for i in range(3):\n",
    "    sum_diagonal += matrix[i][i]\n",
    "\n",
    "for i in range(3):\n",
    "    sum_diagonal += matrix[i][2 - i]\n",
    "\n",
    "sum_diagonal -= matrix[1][1]\n",
    "\n",
    "print(\"该矩阵对角线元素之和为:\", sum_diagonal)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "有n个整数，使其前面各数顺序向后移m个位置，最后m个数变成最前面的m个数。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "移动后的整数序列为: [4, 45, 9, 9, 8]\n"
     ]
    }
   ],
   "source": [
    "n = int(input(\"请输入整数的个数n: \"))\n",
    "m = int(input(\"请输入向后移动的位置数m: \"))\n",
    "m %= n\n",
    "nums = []\n",
    "for _ in range(n):\n",
    "    num = int(input(\"请输入一个整数: \"))\n",
    "    nums.append(num)\n",
    "\n",
    "part1 = nums[-m:]\n",
    "part2 = nums[:-m]\n",
    "\n",
    "result = part1 + part2\n",
    "\n",
    "print(\"移动后的整数序列为:\", result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "编写一个函数，输入n为偶数时，调用函数求1/2+1/4+...+1/n,当输入n为奇数时，调用函数1/1+1/3+...+1/n(利用指针函数)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.5333333333333332\n"
     ]
    }
   ],
   "source": [
    "def calculate_sum(n, result):\n",
    "    \"\"\"\n",
    "    根据输入整数n的奇偶性计算对应数列的和，并将结果存放在result列表中\n",
    "\n",
    "    参数:\n",
    "    n (int): 输入的整数，用于确定数列的项数范围\n",
    "    result (list): 长度为1的列表，用于存放最终的求和结果，模拟指针函数的效果\n",
    "    \"\"\"\n",
    "    if n % 2 == 0:\n",
    "        for i in range(2, n + 1, 2):\n",
    "            result[0] += 1 / i\n",
    "    else:\n",
    "        for i in range(1, n + 1, 2):\n",
    "            result[0] += 1 / i\n",
    "\n",
    "\n",
    "num = int(input(\"请输入一个整数: \"))\n",
    "sum_result = [0] \n",
    "calculate_sum(num, sum_result)\n",
    "print(sum_result[0])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
